$(document).ready(function(){
    
    var month = (new Date()).getMonth();
    var year = (new Date()).getFullYear();
    var disabledMonths = [];
    
    for (var i = 1; i <= month; i++){
        disabledMonths.push(i);
    }
    
    $('#datePickerText').monthpicker({
        selectedYear: year,
        startYear: year,
        finalYear: year+3,
        openOnFocus: false,
        pattern: 'yyyy/mm'
    }).bind('monthpicker-hide',function(){
        if ($('#datePickerText').val()){
            window.location = getBaseURL()+"meal_allocation/meal_allocation/month/"+$('#datePickerText').val();
        }
    }).bind('monthpicker-change-year',function(e,y){
        if (y == year){
            $('#datePickerText').monthpicker('disableMonths', disabledMonths);
        }
        else{
            $('#datePickerText').monthpicker('disableMonths', []);
        }
    });
    
    
    $('#datePickerText').monthpicker('disableMonths', disabledMonths);
    
    $('#datePicker').click(function(){
        $('#datePickerText').monthpicker('show');
    });
    
    //Check if there is a today element
    if ($("table.calendar .today").length > 0){
        $("table.calendar td a").each(function(){
            var today = $(this).parent('.today').length > 0;
            if ($('#is_admin').val() == 1 && today){
                return false;
            }
            $(this).removeAttr('href');
            $(this).parent().addClass('non_editable');
            if (today){
                return false;
            }
            $(this).find('.meals').html('');
        });
    }
});